//
//  AddAddressViewController.m
//  Mahjong
//
//  Created by Chen on 16/9/18.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "AddAddressViewController.h"
#import "AddAddressTableViewCell.h"
#import "AppTools.h"
#import "AddressChoicePickerView.h"
static NSString *identifierForAddAddressTableViewCell = @"AddAddressTableViewCell";

@interface AddAddressViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property (nonatomic , strong) UITableView *tableView;

@property (nonatomic , strong) NSArray *titleArray;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *tel;
@property (nonatomic, strong) NSString *di;
@property (nonatomic, strong) NSString *jie;
@property (nonatomic, strong) NSString *xiang;

@property (nonatomic, strong) UITextField *nameText;
@property (nonatomic, strong) UITextField *telText;
@property (nonatomic, strong) UILabel *diText;
@property (nonatomic, strong) UITextField *jieText;

@end

@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"添加地址";
    
    [self createTitleArray];
    
    [self createTableView];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)backAction{
    
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=InsertAddress&city=%@&street=%@&name=%@&tel=%@&address=%@&userid=%@",self.diText.text,self.jieText.text,self.nameText.text,self.telText.text,self.xiang,[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"]] success:^(id result) {
        NSLog(@"%@",result);
        
    } error:^(NSError *error) {
        
    }];
    
    
    
    
    [self.navigationController popViewControllerAnimated:YES];




}


- (void)createTitleArray{
    self.titleArray = @[@"收货人",@"联系电话",@"所在地区",@"街道"];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - kNavigationBarHeight) style:(UITableViewStyleGrouped)];
    
    self.tableView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:identifierForAddAddressTableViewCell bundle:nil] forCellReuseIdentifier:identifierForAddAddressTableViewCell];
    
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.titleArray.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            cell.textLabel.text = @"联系人";
            self.nameText = [[UITextField alloc] initWithFrame:CGRectMake(100, 15, SCREEN_WIDTH-100, 30)];
            self.nameText.placeholder = @"请输入姓名";
            [cell.contentView addSubview:self.nameText];
            return cell;

        }
        if (indexPath.row == 1) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            cell.textLabel.text = @"联系电话";
            
            self.telText = [[UITextField alloc] initWithFrame:CGRectMake(100, 15, SCREEN_WIDTH-100, 30)];
self.telText.placeholder = @"请输入电话";
            [cell.contentView addSubview:self.telText];
            return cell;
            
        }
        if (indexPath.row == 2) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            cell.textLabel.text = @"所在地区";
            
            self.diText = [[UILabel alloc] initWithFrame:CGRectMake(100, 15, SCREEN_WIDTH-100, 30)];
            self.diText.text = @"请选择地区";

            [cell.contentView addSubview:self.diText];
            return cell;
            
        }
        else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            cell.textLabel.text = @"街道";
            
            self.jieText = [[UITextField alloc] initWithFrame:CGRectMake(100, 15, SCREEN_WIDTH-100, 30)];
            self.jieText.placeholder = @"请输入街道";

            [cell.contentView addSubview:self.jieText];
            return cell;
            
        }
        
        
//        if (indexPath.row == 2 || indexPath.row == 3) {
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//        } else {
//            cell.accessoryType = UITableViewCellAccessoryNone;
//        }
    } else {
        AddAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForAddAddressTableViewCell forIndexPath:indexPath];
        cell.xiangText.delegate = self;
        return cell;
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    self.xiang = text;
    return YES;
//    if ([text isEqualToString:@""] && range.length > 0) {
//        //删除字符肯定是安全的
//        return YES;
//    }
//    else {
//        if (textView.text.length - range.length + text.length > 140) {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"超出最大可输入长度" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];//如果输入的文字大于140 则提示 <span style="font-family: Arial, Helvetica, sans-serif;">"超出最大可输入长度" 并不能继续输入文字</span>
//            
//            [alert show];
//            return NO;
//        }
//        else {
//            return YES;
//        }
//    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==2) {
        AddressChoicePickerView *add = [[AddressChoicePickerView alloc] init];
        add.block = ^(AddressChoicePickerView *view,UIButton *btn,AreaObject *locate){
            self.diText.text = [NSString stringWithFormat:@"%@",locate];
//            self.addressLabel.text = [NSString stringWithFormat:@"%@",locate];
//            self.addressLabel.textColor = [UIColor blackColor];
        };
        [add show];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 60;
    }
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //设置separatorInset(iOS7之后)
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //设置separatorInset(iOS7之后)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
